'use strict';

const Service = require('egg').Service;

class StayMsgService extends Service {
  async create() {
    const { app, ctx } = this;
    const { StayMsg } = app.model.Tables.Web;
    const data = ctx.request.body;

    const res = await StayMsg.create({
      content: data.content,
      projectId: data.projectId,
    });

    return res;
  }

  async get() {
    const { app, ctx } = this;
    const { ProjectAdmin, StayMsg, Project, Admin } = app.model.Tables.Web;
    const data = ctx.query;
    // 获取token
    const token = ctx.get('authorization');
    // const adminRes = await Admin.findOne({
    //   where: {
    //     access_token: token,
    //   },
    // });
    // 根据token查找项目id
    // const projectRes = await ProjectAdmin.findOne({
    //   where: {
    //     adminId: adminRes.dataValues.id,
    //   },
    // });
    const res = await StayMsg.findAndCountAll({
      limit: Number(data.limit) || 10,
      offset: Number(data.offset) || 0,
      include: { model: Project },
      where: {
        projectId: ctx.adminInfo.project_id,
      },
    });
    console.log(res);
    return res;
  }
}
module.exports = StayMsgService;
